package com.greenline.search.test.test.queryanalysis;

import com.greenline.search.test.dao.AnalyzeObject;
import com.greenline.search.test.dao.Disease;
import com.greenline.search.test.dao.DiseaseDao;
import com.greenline.search.test.utils.FileUtils;
import com.greenline.search.test.utils.JSONStreamParserForAnalyze;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 13-12-23
 * Time: 下午3:49
 * To change this template use File | Settings | File Templates.
 */
public class DiseaseSearchFrequency {
    private static Logger logger = LoggerFactory.getLogger(DiseaseSearchFrequency.class);
    private static String baseURL = "http://192.168.1.37:8098/analyze/?q=";

    public static void main(String[] args) throws IOException {
        String classPath = FileUtils.class.getResource("/").getPath();
        String filePath = classPath+"dic/user_keyword_20140115_all.txt";
        List<String> keywordList = FileUtils.readFileAsString(filePath,"GBK");
        long startTime = System.currentTimeMillis();
        StringBuffer sb = new StringBuffer();
        int count = 0;
        Map<String,String> wordAndFrequencyMap = new HashMap<String, String>();
        for(String keyword : keywordList){
            if(count++ % 1000 == 0){
                System.out.println("Deal with " + count + " keywords already.");
            }
            String[] wordAndFrequency = keyword.split("\t");
            if(wordAndFrequency.length == 2){
                wordAndFrequencyMap.put(wordAndFrequency[0],wordAndFrequency[1]);
            }
        }
        DiseaseDao diseaseDao = new DiseaseDao();
        List<Disease> diseaseList = diseaseDao.getAllDisease();
        for(Disease disease : diseaseList){
            int nameCount = 0,commonNameCount = 0;
            if(wordAndFrequencyMap.get(disease.getDiseaseName()) != null){
                nameCount = Integer.parseInt(wordAndFrequencyMap.get(disease.getDiseaseName()));
            }
            if(wordAndFrequencyMap.get(disease.getDiseseCommonName()) != null){
                commonNameCount = Integer.parseInt(wordAndFrequencyMap.get(disease.getDiseseCommonName()));
            }
            sb.append(disease.getDiseaseName()).append("\t").
                    append(nameCount+commonNameCount).append("\n");
        }
        logger.info(sb.toString());
        System.out.println("test end...");
        long costTime = System.currentTimeMillis() - startTime;
        System.out.println("The totalTime is " + (costTime/1000) + " seconds.");
    }
}
